﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication1.课堂练习3
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        string UserIP = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            UserIP = Request.UserHostAddress.ToString();
            Label_ip.Text = "您的ip是："+ UserIP ;

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            HttpCookie oldcookie = Request.Cookies["voted"];
            if(oldcookie == null)
            {

                string tosay = "";

                if (RadioButtonList1.SelectedValue != null)
                {
                    string selected = RadioButtonList1.SelectedValue;
                    tosay = "您投票给了" + selected;

                    HttpCookie newcookie = new HttpCookie("voted");
                    newcookie.Values.Add("ip", UserIP);
                    DateTime d = DateTime.Now;
                    d = d.AddDays(30);
                    newcookie.Expires = d;
                    Response.AppendCookie(newcookie);
                }
                else
                {
                     tosay = "您没有投票！";
                }

                Response.Write("<script>alert(\"" + tosay + "\") </script>");

            }
            else
            {
                Label_ip.Text ="ip used"+ oldcookie.Values["ip"];

                Response.Write("<script>alert(\"您30天内经投过票了\") </script>");
            }
        }
    }
}